"use strict";
// 交易模块数据获取
import Reflux from "reflux";

import hash from "object-hash";
import lscache from "lscache";

import { TradingActions,TradingDetailActions } from "../action/tradingaction.js";
import { StoreMixin } from "./common.js";
import { getISODate } from "../common/datehelper.js";
import { ajaxCall } from "../common/apihelper.js";



var TradingStore = Reflux.createStore({
    // 获取交易页面所有数据
    mixins: [ StoreMixin ],
    listenables: [TradingActions],
    cacheKey: 'stores.tradingstore',
    params: {
        sign:'ABB89E0ADAE0B1B16D6E06DBE95ACF1E', 
	    dimdate:'DAY', 
        // startdate: '20140930',//getISODate(new Date()).replace(/\-/g, ''), 
        startdate: getISODate(new Date()).replace(/\-/g, ''), 
        // enddate: '20140930',//getISODate(new Date()).replace(/\-/g, ''),
        enddate: getISODate(new Date()).replace(/\-/g, ''),
        ds_trade_t1_tb_comparetype: 'YOY',
        ds_trade_t1_hb_comparetype: 'DOD',
        needback: 1,
        ds_comr_volumes_startdate: '20140915',
        ds_portion_volumes_startdate: '20140915'
    },
    apis: {
        // 'ds_trade_t0': {params: ['sign']},
        'ds_trade_t1_tb': {name:'ds_trade_t1', params: ['sign', 'dimdate', 'startdate', 'enddate', 'comparetype', 'needback']},
        'ds_trade_t1_hb': {name:'ds_trade_t1', params: ['sign', 'dimdate', 'startdate', 'enddate', 'comparetype', 'needback']},
    },
    apis_aum: {
        'ds_aum_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate', 'needback']}
    },
    apis_tradecust: {
        'ds_tradecust_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate', 'needback']}
    },
    apis_trade: {
        'ds_trade_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate', 'needback']}
    },
    apis_comi: {
        'ds_comi_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate', 'needback']}
    },
    apis_comr: {
        'ds_comr_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate', 'needback']}
    },
    apis_portion: {
        'ds_portion_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate', 'needback']}
    },
    apis_cash: {
        'ds_cash_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate', 'needback']}
    },
    apis_twof: {
        'ds_twof_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate', 'needback']}
    },
    apis_position: {
        'ds_position_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate', 'needback']}
    },
    apis_repurchase: {
        'ds_repurchase_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate', 'needback']}
    },
    apis_pledge: {
        'ds_pledge_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate', 'needback']}  
    },
    apis_board: {
        'ds_board_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate', 'needback']}
    },
    onT0Fetch: function(callback1){
        // 获取 T0 数据
        var callback = function(response){
            if (callback1) {
                callback1();
                setTimeout(function() {
                    this.pack('ds_trade_t0', response);         
                }.bind(this), 100);
            } else {
                this.pack('ds_trade_t0', response);
            }
        }.bind(this);

        ajaxCall('/v1/exchange/data/query/ds_trade_t0', {}, callback);
    },
    fakeData: function(key, api_name, params){
        // 伪造数据, 测试用
 /*       var data = null;
        switch(api_name){
            case 'ds_trade_t0':
            	data = {
            		"trade_volume": 417666224.17,
                    "trade_volume_date": 1448348410497,
                    "commission_income": 0,
                    "commission_income_date": 1448348432999,
                    "commission_rate": 0,
                    "commission_rate_date": 1448348410750,
                    "position_value": 79332843482.338,
                    "position_value_date": 1448348410884,
                    "cash_deposit": 129710891160069.5,
                    "cash_deposit_date": 1448348412799,
                    "trade_customer_date": 1448348432999,
                    "two_financial_fund_date": 1448348432999,
                    "repurchase_date": 1448348432999,
                    "stock_pledge_date": 1448348432999,
                    "aum_total_amt": 129790224003551.83,
                    "aum_date": 1448348432000,
                    "qdc_rn": 1
            	}
            	break;
            case 'ds_trade_t1':
                if (params.comparetype == 'YOY') {
                    data = {
                    "comparetype":"YOY",
                    "value":[{   
                        "trade_volume": 820000000000,
                        "commission_income": 10000000,
                        "commission_rate": 2800000,
                        "position_value": 37000000,
                        "cash_deposit": 2600000,
                        "trade_customer": 2500000,
                        "two_financial_fund": 3000000,
                        "agreed_repurchase": 1600000,
                        "stock_pledge": 2700000,
                        "aum_total_amt": 2433856.12,
                        "aum_fund_amt": 1533856.12,
                        "aum_shares_amt": 2641014.06,
                        "aum_cash_amt": 3631014.06
                      }],
                    "comparevalue":[{   
                        "trade_volume": 0.092,
                        "commission_income": 0.01249,
                        "commission_rate": 0.032,
                        "position_value": 0.011,
                        "cash_deposit": 0.052,
                        "trade_customer": 0.023,
                        "two_financial_fund": 0.054,
                        "agreed_repurchase": 0.025,
                        "stock_pledge": 0.056,
                        "aum_total_amt": 0.02314174,
                        "aum_fund_amt": 0.01183856,
                        "aum_shares_amt": 0.08141014,
                        "aum_cash_amt": 0.02679804
                      }]
                    }
                } else {
                    data = {
                    "comparetype":"DOD",
                    "value":[{   
                        "trade_volume": 930000000000,
                        "commission_income": 10490000,
                        "commission_rate": 3900000,
                        "position_value": 16000000,
                        "cash_deposit": 5000000,
                        "trade_customer": 2100000,
                        "two_financial_fund": 5000000,
                        "agreed_repurchase": 2100000,
                        "stock_pledge": 5000000,
                        "aum_total_amt": 1183856.12,
                        "aum_fund_amt": 1183856.12,
                        "aum_shares_amt": 8141014.06,
                        "aum_cash_amt": 8141014.06
                      }],
                    "comparevalue":[{   
                        "trade_volume": 0.062,
                        "commission_income": 0.02249,
                        "commission_rate": 0.022,
                        "position_value": 0.033,
                        "cash_deposit": 0.025,
                        "trade_customer": 0.027,
                        "two_financial_fund": 0.045,
                        "agreed_repurchase": 0.036,
                        "stock_pledge": 0.048,
                        "aum_total_amt": 0.0534174,
                        "aum_fund_amt": 0.0125856,
                        "aum_shares_amt": 0.04531014,
                        "aum_cash_amt": 0.02439804
                      }]
                    }
                }
                
                break;
            case 'ds_trade_volumes':
                data = {
                "ex_userum": "DINGHAIJIANG981",
                "ex_branch_code": "1",
                "ex_branch_name": "证券总部零售业务事业部",
                "value": [
                  {
                    "value": 5096012905.23,
                    "yyyymmdd": 20151012,
                    "label": "两融成交量",
                    "vdate": 20151012,
                    "count": 1
                  },
                  {
                    "value": 45094787855.15,
                    "yyyymmdd": 20151012,
                    "label": "传统成交量",
                    "vdate": 20151012,
                    "count": 1
                  },
                  {
                    "value": 4356381363.93,
                    "yyyymmdd": 20151013,
                    "label": "两融成交量",
                    "vdate": 20151013,
                    "count": 1
                  },
                  {
                    "value": 42673840548.87,
                    "yyyymmdd": 20151013,
                    "label": "传统成交量",
                    "vdate": 20151013,
                    "count": 1
                    }]
                }
                break;
            case 'ds_comi_volumes':
                data = {
                    "comparetype":"YOY",
                    "value":[{
                        "label": "两融佣金收入",
                        "value": 3200000,
                        "vdate": 1430150400000
                      },
                     {
                        "label": "传统佣金收入",
                        "value": 1600000,
                        "vdate": 1430150400000
                     }], 
                    "comparevalue":[{
                        "label": "两融佣金收入",
                        "value": 0.22,
                        "vdate": 1430150400000
                      },
                     {
                        "label": "传统佣金收入",
                        "value": 0.33,
                        "vdate": 1430150400000
                     }]
                }
                break;
            case 'ds_comr_volumes':
                data = {
                    "comparetype":"YOY",
                    "value":[{
                        "label": "两融佣金比例量",
                        "value": 460000000000,
                        "vdate": 1430150400000
                    }], 
                    "comparevalue":[{
                        "label": "两融佣金比例量",
                        "value": 460000000000,
                        "vdate": 1430150400000
                    }]
                }
                break;
            case 'ds_cash_volumes':
                data = {
                    "comparetype":"YOY",
                    "value":[{
                        "label": "两融保证金比例量",
                        "value": 460000000000,
                        "vdate": 1430150400000
                    }], 
                    "comparevalue":[{
                        "label": "两融保证金比例量",
                        "value": 460000000000,
                        "vdate": 1430150400000
                    }]
                }
                break;
            case 'ds_customer_volumes':
                data = {
                    "comparetype":"YOY",
                    "value":[{
                        "label": "两融交易客户数比例量",
                        "value": 460000000000,
                        "vdate": 1430150400000
                    }], 
                    "comparevalue":[{
                        "label": "两融交易客户数比例量",
                        "value": 460000000000,
                        "vdate": 1430150400000
                    }]
                }
                break;
            case 'ds_twof_volumes':
                data = {
                    "comparetype":"YOY",
                    "value":[{
                        "label": "两融量",
                        "value": 460000000000,
                        "vdate": 1430150400000
                     }], 
                    "comparevalue":[{
                        "label": "两融量",
                        "value": 460000000000,
                        "vdate": 1430150400000
                     }]
                }
                break;
            case 'ds_repurchase_volumes':
                data = {
                    "comparetype":"YOY",
                    "value":[{
                        "label": "两融回购量",
                        "value": 460000000000,
                        "vdate": 1430150400000
                    }], 
                    "comparevalue":[{
                        "label": "两融回购量",
                        "value": 460000000000,
                        "vdate": 1430150400000
                     }]
                }
                break;
            case 'ds_pledge_volumes':
                data = {
                    "comparetype":"YOY",
                    "value":[{
                        "label": "两融质押量",
                        "value": 460000000000,
                        "vdate": 1430150400000
                     }], 
                    "comparevalue":[{
                        "label": "两融质押量",
                        "value": 460000000000,
                        "vdate": 1430150400000
                     }]
                }
                break;
        }*/
 //       this.pack(key, data);
    }
})


var TradingDetailStore = Reflux.createStore({
    // 获取交易页面所有数据
    mixins: [ StoreMixin ],
    listenables: [TradingDetailActions],
    cacheKey: 'stores.tradingdetailstore',
    params: {
        sign:'ABB89E0ADAE0B1B16D6E06DBE95ACF1E', 
        dimdate:'DAY', 
        startdate: '20150922', 
        enddate: '20150925',
        ds_trade_t1_tb_comparetype: 'YOY',
        ds_trade_t1_hb_comparetype: 'DOD',
//        ds_trade_volumes_comparetype: 'DOD',
//        ds_trade_volumes_startdate: '', // 从 enddate 往前推四 天/周/月/季/年
    },
    apis: {

    },
    apis_aum: {
        // 主动传入参数调用的API 列表
        // 调用方法  fetchData({apiCollections: ['aum'], dimdate: 'MONTH'})
        'ds_aum_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_aum_brch_rank':{params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_aum_area_rank':{params: ['sign', 'dimdate', 'startdate', 'enddate']},
    },
    apis_trade: {
        'ds_trade_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_trade_brch_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_trade_area_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']}
    },
    apis_comi: {
        'ds_comi_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_comi_brch_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_comi_area_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']}
    },
    apis_comr: {
        'ds_comr_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_comr_brch_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_comr_area_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']}
    },
    apis_portion: {
        'ds_portion_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_portion_brch_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_portion_area_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']}
    },
    apis_position: {
        'ds_position_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_position_brch_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_position_area_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']}
    },
    apis_cash: {
        'ds_cash_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_cash_brch_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_cash_area_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']}
    },
    apis_tradecust: {
        'ds_tradecust_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_tradecust_brch_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_tradecust_area_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']}
    },
    apis_twof: {
        'ds_twof_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_twof_brch_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_twof_area_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']}
    },
    apis_repurchase: {
        'ds_repurchase_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_repurchase_brch_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_repurchase_area_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']}
    },
    apis_pledge: {
        'ds_pledge_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_pledge_brch_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_pledge_area_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']}
    },
    apis_board: {
        'ds_board_volumes': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_board_brch_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']},
        'ds_board_area_rank': {params: ['sign', 'dimdate', 'startdate', 'enddate']}
    },
    fakeData: function(key, api_name, params){
        // 伪造数据, 测试用
        var data = null;
        switch(api_name){
            case 'ds_trade_volumes':
                data = {
                "ex_userum": "DINGHAIJIANG981",
                "ex_branch_code": "1",
                "ex_branch_name": "证券总部零售业务事业部",
                "value": [
                {
                    "value": 5096012905.23,
                    "yyyymmdd": 20151012,
                    "label": "两融成交量",
                    "vdate": 20151012,
                    "count": 1
                },
                {
                    "value": 45094787855.15,
                    "yyyymmdd": 20151012,
                    "label": "传统成交量",
                    "vdate": 20151012,
                    "count": 1
                },
                {
                    "value": 4356381363.93,
                    "yyyymmdd": 20151013,
                    "label": "两融成交量",
                    "vdate": 20151013,
                    "count": 1
                },
                {
                    "value": 42673840548.87,
                    "yyyymmdd": 20151013,
                    "label": "传统成交量",
                    "vdate": 20151013,
                    "count": 1
                }]
                }
                break;
        }
        this.pack(key, data);
    }
})

export { TradingStore, TradingDetailStore };